package com.example.block_chain.controller.award;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.block_chain.common.AppHttpCodeEnum;
import com.example.block_chain.common.ResponseResult;
import com.example.block_chain.domain.dto.AwardDto;
import com.example.block_chain.domain.pojo.Award;
import com.example.block_chain.domain.pojo.Student;
import com.example.block_chain.domain.vo.AwardVo;
import com.example.block_chain.service.award.AwardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/*
 *@description: 奖项相关接口
 *@author: hisen
 *@Time: 2023/11/23  10:20
 */
@RestController
@Slf4j
public class AwardController {
    @Resource
    AwardService awardService;

    @PostMapping("/uploadAward")
    public ResponseResult websocket(@ModelAttribute AwardDto awardDto, HttpSession session) throws IOException {
        Student student = (Student) session.getAttribute("student");
        if (student == null) return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        BeanUtils.copyProperties(student, awardDto);
        awardService.saveAward(awardDto);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @GetMapping("/getAward")
    public ResponseResult getAward(@RequestParam Long id) throws UnsupportedEncodingException {
        AwardVo awardVo = awardService.getAwardVoById(id);
        if (awardVo == null) return ResponseResult.errorResult(AppHttpCodeEnum.AWARD_NO_EXISTS);
        return ResponseResult.okResult(awardVo);
    }

    @GetMapping("/getAwards")
    public ResponseResult getAwards(@RequestParam Long id) throws UnsupportedEncodingException {
        Award award = awardService.getAward(id);
        return ResponseResult.okResult(award);
    }

    @GetMapping("/getAllAwards")
    public ResponseResult getAllAwards(@RequestParam String sNo, @RequestParam String schoolCode, @RequestParam Integer page, @RequestParam Integer pageSize) throws UnsupportedEncodingException {
        Page<AwardVo> awardPage = awardService.getAwards(sNo, schoolCode, page, pageSize);
        return ResponseResult.okResult(awardPage);
    }
}
